package com.aispeech.rcprvd.provider;

import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;

import com.aispeech.rcprvd.jni.RcNativeProcessJni;
import com.aispeech.rcprvd.provider.base.IDataProvider;
import com.aispeech.rcprvd.utils.RcPrvdConst;

public class NanoBleNodeDataProvider implements IDataProvider {
    private final String TAG = "NanoBleNodeDataProvider";
    private HandlerThread mReadThread;
    private Handler mHandler;

    public NanoBleNodeDataProvider() {
        mReadThread = new HandlerThread("BRT");//ble read thread
        mReadThread.start();
        mHandler = new Handler(mReadThread.getLooper());

    }

    private Runnable mBleReadRunnable = new Runnable() {
        @Override
        public void run() {
            int ret = RcNativeProcessJni.native_process_start(RcPrvdConst.RCDevSource.NANO_BLE_NODE);
            if (ret != RcPrvdConst.ErrorCode.NO_ERROR) {
                Log.e(TAG, "native_process_start err : " + ret);
                return;
            }
            byte[] tmp = new byte[1];
            Log.d(TAG, "BleReadThread run: ---------------");
            RcNativeProcessJni.native_process_feed(RcPrvdConst.RCDevSource.NANO_BLE_NODE, tmp, 0);
            Log.d(TAG, "BleReadThread over: ---------------");
        }
    };


    @Override
    public int start() {
        Log.d(TAG, "start: ");
        mHandler.post(mBleReadRunnable);
        return RcPrvdConst.ErrorCode.NO_ERROR;
    }

    @Override
    public int stop() {
        Log.d(TAG, "stop: ");
        int ret = RcNativeProcessJni.native_process_stop(RcPrvdConst.RCDevSource.NANO_BLE_NODE);
        if (ret < 0) {
            Log.e(TAG, "native_process_stop err : " + ret);
            return RcPrvdConst.ErrorCode.NATIVE_ERR;
        }
        return RcPrvdConst.ErrorCode.NO_ERROR;
    }
}
